package com.example.tool.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tool.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liangkun on 2018/8/20.
 * 作用:运动统计数据view
 */

public class SportCountMinCardView extends LinearLayout {
    @BindView(R.id.bar_round_chart)
    protected BarRoundChartView barRoundChartView; //运动统计总分钟
    @BindView(R.id.rl_single_bar)
    protected RelativeLayout rlSingle; //运动统计父控件
    private LinearLayout llSingle;
    private List<Float> singlelist;
    private Context context;
    public SportCountMinCardView(Context context) {
        this(context, null);

    }

    public SportCountMinCardView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SportCountMinCardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.activity_item_count_min, this);
        ButterKnife.bind(this);
        barRoundChartView.setListener((number, x, y) -> {
            rlSingle.removeView(llSingle);
            llSingle = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.layout_count_bar_bg, null);
            TextView tvMoney = (TextView) llSingle.findViewById(R.id.tv_shouru_pro);

            tvMoney.setText("" + (singlelist.get(number)));
            llSingle.measure(0, 0);

            //得到父控件的宽高
            int measuredWidth = llSingle.getMeasuredWidth();
            int measuredHeight = llSingle.getMeasuredHeight();

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.leftMargin = (int) x - measuredWidth / 2;
            params.topMargin = (int) ((int) y - measuredHeight / 2 - context.getResources().getDimension(R.dimen.sw_dp_15));

            llSingle.setLayoutParams(params);
            rlSingle.addView(llSingle);
        });

    }

    public void setMinData(List<Float> minCountData) {
        this.singlelist = minCountData;
        barRoundChartView.setList(singlelist);
    }
}
